package com.suning.sawp.service.impl.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.dto.returngoods.OosStatisRtn;
import com.suning.sawp.intf.dao.BiStoreOosDaoService;
import com.suning.sawp.po.bi.BiStoreOosDetailEntity;
import com.suning.sawp.po.bi.BiStoreOosEntity;

@Service("biStoreOosDaoService")
public class BiStoreOosDaoServiceImpl implements BiStoreOosDaoService {

    // 汇总
    private static final String SUM = "1";
    // 不汇总
    private static final String NO_SUM = "0";
    
    @Resource
    DalClient dalClient;
    
    @Override
    public OosStatisRtn queryStoreGeneralOosStatis(String storeCode) {
        return queryStoreOosStatis(storeCode, "biStoreGeneralOos.QUERY_STORE_GENERAL_OOS");
    }

    @Override
    public List<OosStatisRtn> queryStoreCateGeneralOosStatis(String storeCode, Set<String> cateCodes) {
        return queryStoreCateOosStatis(storeCode, cateCodes, "biStoreGeneralOos.QUERY_STORE_CATE_GENERAL_OOS");
    }

    @Override
    public OosStatisRtn queryStoreHotOosStatis(String storeCode) {
        return queryStoreOosStatis(storeCode, "biStoreHotOos.QUERY_STORE_HOT_OOS");
    }

    @Override
    public List<OosStatisRtn> queryStoreCateHotOosStatis(String storeCode, Set<String> cateCodes) {
        return queryStoreCateOosStatis(storeCode, cateCodes, "biStoreHotOos.QUERY_STORE_CATE_HOT_OOS");
    }

    @Override
    public OosStatisRtn queryStoreMainPushOosStatis(String storeCode) {
        return queryStoreOosStatis(storeCode, "biStoreMainPushOos.QUERY_STORE_MAIN_PUSH_OOS");
    }

    @Override
    public List<OosStatisRtn> queryStoreCateMainPushOosStatis(String storeCode, Set<String> cateCodes) {
        return queryStoreCateOosStatis(storeCode, cateCodes, "biStoreMainPushOos.QUERY_STORE_CATE_MAIN_PUSH_OOS");
    }
    
    /**
     * 
     * 功能描述: 查询门店缺货统计<br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @param sqlId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private OosStatisRtn queryStoreOosStatis(String storeCode, String sqlId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        OosStatisRtn list = dalClient.queryForObject(sqlId, paramMap, OosStatisRtn.class);
        return list;
    }
    
    private List<OosStatisRtn> queryStoreCateOosStatis(String storeCode, Set<String> cateCodes, String sqlId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        paramMap.put("cateCodes", cateCodes);
        List<OosStatisRtn> list = dalClient.queryForList(sqlId, paramMap, OosStatisRtn.class);
        return list;
    }

    @Override
    public List<BiStoreOosEntity> queryStoreGeneralOosBrandStatis(String storeCode, Set<String> cateCodes) {
        return queryBrandList(storeCode, cateCodes, "biStoreGeneralOos.QUERY_STORE_OOS_BRANDS");
    }

    @Override
    public List<BiStoreOosEntity> queryStoreHotOosBrandStatis(String storeCode, Set<String> cateCodes) {
        return queryBrandList(storeCode, cateCodes, "biStoreHotOos.QUERY_STORE_OOS_BRANDS");
    }

    @Override
    public List<BiStoreOosEntity> queryStoreMainPushOosBrandStatis(String storeCode, Set<String> cateCodes) {
        return queryBrandList(storeCode, cateCodes, "biStoreMainPushOos.QUERY_STORE_OOS_BRANDS");
    }
    
    private List<BiStoreOosEntity> queryBrandList(String storeCode, Set<String> cateCodes, String sqlId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        paramMap.put("cateCodes", cateCodes);
        return dalClient.queryForList(sqlId, paramMap, BiStoreOosEntity.class);
    }

    @Override
    public List<BiStoreOosEntity> queryStoreOosGeneralCateBrandStatis(String storeCode, Set<String> cateCodes,
            Set<String> brandCodes, String sumType, int startIndex, int limit) {
        return queryStoreOosCateBrandStatis(storeCode, cateCodes, brandCodes, sumType, "biStoreGeneralOos", startIndex, limit);
    }

    @Override
    public List<BiStoreOosEntity> queryStoreOosHotCateBrandStatis(String storeCode, Set<String> cateCodes,
            Set<String> brandCodes, String sumType, int startIndex, int limit) {
        return queryStoreOosCateBrandStatis(storeCode, cateCodes, brandCodes, sumType, "biStoreHotOos", startIndex, limit);
    }

    @Override
    public List<BiStoreOosEntity> queryStoreOosMainPushCateBrandStatis(String storeCode, Set<String> cateCodes,
            Set<String> brandCodes, String sumType, int startIndex, int limit) {
        return queryStoreOosCateBrandStatis(storeCode, cateCodes, brandCodes, sumType, "biStoreMainPushOos", startIndex, limit);
    }
    
    private List<BiStoreOosEntity> queryStoreOosCateBrandStatis(String storeCode, Set<String> cateCodes,
            Set<String> brandCodes, String sumType, String sqlNamespace, int startIndex, int limit) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        paramMap.put("startIndex", startIndex);
        paramMap.put("limit", limit);
        if (CollectionUtils.isNotEmpty(brandCodes)) {
            paramMap.put("brandCodes", brandCodes);
            // 按品牌查询
            return dalClient.queryForList(sqlNamespace + ".QUERY_OOS_BRAND_STATIS", paramMap, BiStoreOosEntity.class);
        }
        if (CollectionUtils.isNotEmpty(cateCodes)) {
            paramMap.put("cateCodes", cateCodes);
        }
        if (StringUtils.isNotBlank(sumType) && NO_SUM.equals(sumType)) {
            // 品类不汇总统计
            return dalClient.queryForList(sqlNamespace + ".QUERY_OOS_CATE_BRAND_STATIS", paramMap, BiStoreOosEntity.class);
        }
        
        return dalClient.queryForList(sqlNamespace + ".QUERY_OOS_CATE_STATIS", paramMap, BiStoreOosEntity.class);
    }

    @Override
    public List<BiStoreOosEntity> queryOosGeneralBrandList(String storeCode, Set<String> brand9codes) {
        return queryOosBrandList(storeCode, brand9codes, "biStoreGeneralOos.QUERY_BRAND_INFO");
    }

    @Override
    public List<BiStoreOosEntity> queryOosHotBrandList(String storeCode, Set<String> brand9codes) {
        return queryOosBrandList(storeCode, brand9codes, "biStoreHotOos.QUERY_BRAND_INFO");
    }

    @Override
    public List<BiStoreOosEntity> queryOosMainpushBrandList(String storeCode, Set<String> brand9codes) {
        return queryOosBrandList(storeCode, brand9codes, "biStoreMainPushOos.QUERY_BRAND_INFO");
    }

    /**
     * 
     * 功能描述: 根据9位编码查询品牌信息<br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @param brand9codes
     * @param sqlId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private List<BiStoreOosEntity> queryOosBrandList(String storeCode, Set<String> brand9codes, String sqlId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        paramMap.put("brand9codes", brand9codes);
        return dalClient.queryForList(sqlId, paramMap, BiStoreOosEntity.class);
    }
    
    @Override
    public List<BiStoreOosEntity> queryOosGeneralBrandList(String storeCode, Set<String> brand4Codes,
            Set<String> cateCodes) {
        return queryOosBrandList(storeCode, brand4Codes, cateCodes, 
                "biStoreGeneralOos.QUERY_OOS_BRAND_BY_CATE_BRAND4CODE");
    }

    @Override
    public List<BiStoreOosEntity> queryOosHotBrandList(String storeCode, Set<String> brand4Codes, Set<String> cateCodes) {
        return queryOosBrandList(storeCode, brand4Codes, cateCodes, 
                "biStoreHotOos.QUERY_OOS_BRAND_BY_CATE_BRAND4CODE");
    }

    @Override
    public List<BiStoreOosEntity> queryOosMainpushBrandList(String storeCode, Set<String> brand4Codes,
            Set<String> cateCodes) {
        return queryOosBrandList(storeCode, brand4Codes, cateCodes, 
                "biStoreMainPushOos.QUERY_OOS_BRAND_BY_CATE_BRAND4CODE");
    }
    
    /**
     * 
     * 功能描述: 查询缺货品牌列表，4位品牌编码和品类编码<br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @param brand4codes
     * @param cateCodes
     * @param sqlId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private List<BiStoreOosEntity> queryOosBrandList(String storeCode, Set<String> brand4codes
            , Set<String> cateCodes, String sqlId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        if (CollectionUtils.isNotEmpty(brand4codes)) {
            paramMap.put("brand4codes", brand4codes);
        }
        if (CollectionUtils.isNotEmpty(cateCodes)) {
            paramMap.put("cateCodes", cateCodes);
        }
        
        return dalClient.queryForList(sqlId, paramMap, BiStoreOosEntity.class);
    }

    @Override
    public List<BiStoreOosDetailEntity> queryOosDetails(String storeCode, Set<String> brand9codes, Set<String> cateCodes,
            String commKeyword, String sortType, int startIndex, int limit, String queryType) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        paramMap.put("startIndex", startIndex);
        paramMap.put("limit", limit);
        paramMap.put("sortType", sortType);
        paramMap.put("queryType", queryType);
        if (CollectionUtils.isNotEmpty(brand9codes)) {
            paramMap.put("brand9codes", brand9codes);
        }
        if (CollectionUtils.isNotEmpty(cateCodes)) {
            paramMap.put("cateCodes", cateCodes);
        }
        if (StringUtils.isNotBlank(commKeyword)) {
            paramMap.put("commKeyword", "%"+commKeyword+"%");
        }
        
        return dalClient.queryForList("biStoreGeneralOos.QUERY_STORE_OOS_DETAILS", paramMap, BiStoreOosDetailEntity.class);
    }
}
